package nongchang.kuaijiecaifu.com.ncapp_android.ui.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import ezy.lite.util.ContextUtil;
import ezy.lite.util.DateTime;
import ezy.lite.util.Param;
import nongchang.kuaijiecaifu.com.ncapp_android.R;
import nongchang.kuaijiecaifu.com.ncapp_android.common.adapter.ItemAdapter;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.Address;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.Calculate;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.UserChangeSelectAddressActivity;

/**
 * Created by Administrator on 2016/4/26 0026.
 */
public class AddressListAdapter extends ItemAdapter<Address, AddressListAdapter.AddressViewHolder> {
    private Context context;
    private AlertDialog.Builder builder;

    public AddressListAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public AddressViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new AddressViewHolder(inflate(R.layout.item_address, parent));
    }

    @Override
    public void onBindViewHolder(AddressViewHolder holder, int position) {
        Address item = getItem(position);
        holder.txtName.setText(item.name);
        holder.txtPhone.setText(item.phone);
        holder.txtAddress.setText(item.address);
        if (item.isdefault) {
            holder.checkIsdefault.setVisibility(View.VISIBLE);
        } else {
            holder.checkIsdefault.setVisibility(View.GONE);
        }
        holder.img_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder = new AlertDialog.Builder(context);
                builder.setMessage("您确定要删除吗？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        remove(position);
                        builder = null;
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        builder = null;
                    }
                });
                //    显示出该对话框
                builder.show();

            }
        });
        holder.img_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContextUtil.startActivity(context, UserChangeSelectAddressActivity.class, Param.bundle(item));
            }
        });
    }


    static class AddressViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_phone)
        TextView txtPhone;
        @BindView(R.id.txt_address)
        TextView txtAddress;
        @BindView(R.id.txt_name)
        TextView txtName;
        @BindView(R.id.img_delete)
        ImageView img_delete;
        @BindView(R.id.img_edit)
        ImageView img_edit;
        @BindView(R.id.check_isdefault)
        ImageView checkIsdefault;

        public AddressViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    ContextUtil.startActivity(v.getContext(), LifeShopActivity.class);
                }
            });
        }

    }
}
